/*
 * Copyright (c) 2013 Andaily Information Technology Co. Ltd
 * www.andaily.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Andaily Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with Andaily Information Technology Co. Ltd.
 */
package top.suven.http.oauth.token;

import top.suven.http.oauth.validator.AbstractOAuthTypeValidator;
import org.apache.oltu.oauth2.as.request.AbstractOAuthTokenRequest;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: AbstractAuthorizeHandler.java
 * @Description: OAUTH2.0的认证服务, auth 请求验证
 * @Author suven.wang
 * @Date 2018/7/26 18:01
 * @Copyright: (c) 2018 gc by https://www.gc.com.cn/
 * @Version : 1.0.0
 *  --------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 *  --------------------------------------------------------
 */
public abstract class AbstractOAuthTokenHandler implements OAuthTokenHandler {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractOAuthTokenHandler.class);

    protected AbstractOAuthTokenRequest tokenRequest;

//    private OauthServiceFacade oauthService;
//    protected AbstractOAuthTypeValidator typeValidator;

    private void init(){
//        oauthService = SpringUtil.getBean(OauthServiceFacade.class);
    }

    @Override
    public boolean support(AbstractOAuthTokenRequest tokenRequest){
        init();
        return true;
    }




    @Override
    public final OAuthResponse handle(AbstractOAuthTokenRequest tokenRequest) throws  OAuthSystemException {
        this.tokenRequest = tokenRequest;
        AbstractOAuthTypeValidator typeValidator = getValidator();
        boolean validate = typeValidator.validate() ;
        if (!validate) {//校验失败,返回失败信息;
            return typeValidator.getResponse();
        }
        handleAfterValidation();
        return typeValidator.getResponse();
    }




    protected String clientId() {
        return tokenRequest.getClientId();
    }



    protected abstract AbstractOAuthTypeValidator getValidator();


    protected abstract OAuthResponse handleAfterValidation();


}
